R の線形回帰モデルで Predict 関数を使用する

您所在的位置:网站首页 r predict函数 R の線形回帰モデルで Predict 関数を使用する

R の線形回帰モデルで Predict 関数を使用する

2024-07-05 07:46| 来源: 网络整理| 查看: 265

lm() 関数を使用して線形回帰モデルを構築した後、それを使用してできることの 1つは、機能の新しい値 (出力または従属とも呼ばれる) 変数の値を予測することです。 入力変数または独立変数)。

この記事では、線形回帰のコンテキストで R の predict() 関数の基本的な引数を見ていきます。 特に、関数は、入力が特定の列名を持つ特定の形式であることを期待していることがわかります。

R の線形回帰モデルで predict() 関数を使用する

predict() 関数を実証するために、まずいくつかのサンプル データを使用して線形回帰モデルを構築します。

データ フレームの列名を観察し、それらが線形回帰式でどのように使用されているかを確認します。

コード例:

Feature = c(15:24) set.seed(654) Response = 2* c(15:24) + 5 + rnorm(10, 0,3) DFR = data.frame(Response, Feature) DFR # The arguments' formula and data are named for clarity. LR_mod = lm(formula = Response~Feature, data = DFR) LR_mod

出力:

> Feature = c(15:24) > set.seed(654) > Response = 2* c(15:24) + 5 + rnorm(10, 0,3) > DFR = data.frame(Response, Feature) > DFR Response Feature 1 32.71905 15 2 35.83089 16 3 44.06888 17 4 40.71729 18 5 43.28590 19 6 47.45182 20 7 50.19730 21 8 51.81954 22 9 53.22364 23 10 51.69406 24 > # The arguments' formula and data are named for clarity. > LR_mod = lm(formula = Response~Feature, data = DFR) > LR_mod Call: lm(formula = Response ~ Feature, data = DFR) Coefficients: (Intercept) Feature 2.096 2.205 predict() を使用して応答を予測する

線形回帰モデルができたので、R の predict() 関数を使用して、特徴変数の新しい値に対応する応答の値を予測できます。

predict() 関数は、線形回帰モデルのために少なくとも 2つの引数を必要とします。

モデル オブジェクト。 新しいデータ。

このコンテキストでは、考慮に入れる必要がある 2つの重要な考慮事項があります。

新しいデータをデータ フレームとして提供する必要があります。 この例では、機能は単一の変数です。

predict() 関数にベクトルを与えると、エラーが発生します。

新しいデータ フレームの機能変数の列名が元のデータ フレームの対応する列の名前と異なる場合、予期しない出力が得られます。

コード例:

# First, let us create new values for the feature variable. NewFeature = c(20.5, 16.5, 22.5) # If we provide a vector, we get an error. predict(object = LR_mod, newdata = NewFeature) # Make a data frame. DFR2 = data.frame(NewFeature) # Another error. # R saw the correct number of rows in the new data but did not use them. predict(LR_mod, newdata = DFR2)

出力:

> # First, let us create new values for the feature variable. > NewFeature = c(20.5, 16.5, 22.5) > # If we provide a vector, we get an error. > predict(object = LR_mod, newdata = NewFeature) Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 'data' must be a data.frame, environment, or list > # Make a data frame. > DFR2 = data.frame(NewFeature) > # Another error. > # R saw the correct number of rows in the new data but did not use them. > predict(LR_mod, newdata = DFR2) 1 2 3 4 5 6 7 8 9 10 35.17674 37.38209 39.58745 41.79280 43.99816 46.20351 48.40887 50.61422 52.81958 55.02494 Warning message: 'newdata' had 3 rows but variables found have 10 rows

predict() 関数から正しい出力を得るには、2つのことを確認する必要があります。

predict() 関数の newdata 引数にデータ フレームを渡す必要があります。 これは、最初のエラーの後に上記で行われました。 機能変数の列名は、モデルを構築するために元のデータ フレームで使用されたものと同じである必要があります。 この変更は、次のコード セグメントで行います。

引数に名前を付けることも良い習慣です。

これらの側面に対処すると、predict() 関数から期待される出力が得られます。

コード例:

# The feature column of the new data frame is given the same name as in the original data frame. DFR3 = data.frame(Feature = NewFeature) # Finally, predict() works as expected. predict(LR_mod, newdata = DFR3)

出力:

> # The feature column of the new data frame is given the same name as in the original data frame. > DFR3 = data.frame(Feature = NewFeature) > # Finally, predict() works as expected. > predict(LR_mod, newdata = DFR3) 1 2 3 47.30619 38.48477 51.71690


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3